<template>
  <!-- <BasicDrawer 
    v-bind="$attrs" 
    @register="registerDrawer" 
    :title="getTitle" 
    width="600px" 
    @ok="handleSubmit" 
    showFooter
    destroyOnClose
  > -->
  <!-- <BasicForm @register="registerForm" /> -->
  <!-- </BasicDrawer> -->
  <div style="padding-left: 20px; padding-right: 20px">
    <BasicForm @register="registerForm" ref="formRef" @value-change="handleValueChange" />
  </div>
</template>
<script lang="ts" setup>
  import { ref, watch, unref, useAttrs, onUnmounted, onMounted } from "vue"
  import { BasicForm, useForm } from "/@/components/Form"
  import { inputFormSchemas } from "./data"
  import { BasicDrawer, useDrawerInner } from "/@/components/Drawer"
  import { useMessage } from "/@/hooks/web/useMessage"
  const { createMessage } = useMessage()

  const emit = defineEmits(["success", "register", "value-change"])
  const attrs = useAttrs()
  const isUpdate = ref(true)
  const record = ref<any>({})
  const formRef = ref()
  const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
    // labelWidth: 90,
    // schemas: inputFormSchemas,
    // showActionButtonGroup: false,
    // wrapperCol: { lg: 24, md: 24 },

    labelWidth: 70,
    labelAlign: "left",
    colon: false,
    schemas: inputFormSchemas,
    showActionButtonGroup: false,
    labelCol: {
      md: { span: 4 },
      sm: { span: 6 },
    },
    compact: true,
    wrapperCol: { lg: 24, md: 24 },
  })

  const props = defineProps({
    data: { type: Object, default: {} },
  })

  onMounted(async () => {
    await handleFormRecord()
  })

  watch(
    () => props.data,
    async (val: any) => {
      await handleFormRecord()
    },
    { immediate: true }
  )

  async function handleFormRecord() {
    if (!props.data || !formRef.value) {
      return
    }

    const { data } = props
    await resetFields()
    record.value = data.record
    const dataSource = data.dataSource
    // setDrawerProps({ confirmLoading: false, loading: true })
    const options = unref(dataSource)
    if (options && options.length > 0) {
      updateSchema({
        field: "datasourceId",
        componentProps: {
          optionFilterProp: "label",
          showSearch: true,
          options,
        },
      })
    }

    try {
      isUpdate.value = !!data?.isUpdate
      // record.value.datasourceId = null
      // if (unref(isUpdate)) {
      //   record.value = await getDiyForm({id: data.record.id}) || {}
      // }
      setFieldsValue(record.value)
    } finally {
      // setDrawerProps({ loading: false})
    }
  }

  const getData = async () => {
    const values = await validate()
    return { ...values }
  }

  const handleValueChange = (values) => {
    console.log("values", values)
    emit("value-change", values)
  }

  // const [registerDrawer, {setDrawerProps, closeDrawer}] = useDrawerInner(async (data) => {

  // });

  // const getTitle = computed(() => (!unref(isUpdate) ? '新增表单' : '编辑表单'));

  // async function handleSubmit() {
  //   try {
  //     let values = await validate();
  //     setDrawerProps({confirmLoading: true});

  //     await saveOrUpdateDiyForm({...values}, unref(isUpdate));
  //     closeDrawer();
  //     createMessage.success(`${getTitle.value}成功`)
  //     emit('success');
  //   } finally {
  //     setDrawerProps({confirmLoading: false});
  //   }
  // }

  defineExpose({
    getData,
  })
</script>
